<?php
/**
 * 前端积分商城页面
 * Created by PhpStorm.
 * User: hls
 * Date: 2016/3/11
 * Time: 14:57
 */
namespace Wap\Controller;
use Common\Model\CouponModel;
use Common\Model\PointModel;
use Common\Model\SignLuckyModel;
use Common\Model\UtilsModel;
use Wap\BaseController;
use Common\Model\UserModel;
use Common\Model\MarqueeModel;
use Common\Model\PointMallModel;


class PointMallController extends BaseController {
    /**
     * 轿辰会积分商城首页
     * Author: shijy
     */
    public function index(){
        self::index_v3();
    }
    /**
     * 列表式
     */
    public function index_list(){
        $marqueeList=MarqueeModel::getMarqueeList(MarqueeModel::MARQUEE_WAP_POINT_MALL,4,1);
        $this->smarty->assign('marquee',$marqueeList);

        $uid = session('uid')+0;
        $list = PointMallModel::getOnlineAndPrepareList(1,8,CLIENT_SHOW_WAP,$uid);

        $this->smarty->assign("list",$list['data']);
        //物品类型列表
        $typeList = PointMallModel::getDeliveryType();
        $this->smarty->assign('typeList',$typeList);

        $pointRuleURL=C('POINT_RULE_URL');
        if(!$pointRuleURL){
            $pointRuleURL=U('Point/rule');
        }
        $this->smarty->assign('POINT_RULE_URL',$pointRuleURL);

        //积分抽奖功能开关
        $switch = S('pointDialLotterySwitch');
        if(!$switch){
            $switch = M('cfg_config',null,DB_MAIN_CFG)->where("cfgName='POINT_LOTTERY_DIAL_SWITCH'")->getField('cfgValue');
        }
        $this->smarty->assign('switch',$switch);

        $this->smarty->assign('title',C('SITE_NAME').'-积分商城');
        $this->smarty->assign('time',time());
        //是否当日首次签到
        signAssignWap($uid,$this->smarty);
        $this->smarty->display('PointMall_v2/pointMall_list.html');
    }

    
    public function index_v3(){
        $marqueeList=MarqueeModel::getMarqueeList(MarqueeModel::MARQUEE_WAP_POINT_MALL,4,1);
        $this->smarty->assign('marquee',$marqueeList);

        $uid = session('uid')+0;


        $goodsTypeArr=PointMallModel::getGoodsType();
        $listByType=array();

        foreach ($goodsTypeArr as $goodsTypeId=>$goodsTypeName){
            $list=PointMallModel::getListByGoodsType($goodsTypeId,1,4);
            if ($list['total']>0){
                $listByType[$goodsTypeId]=$list['data'];
            }
        }


        //pre_dump($listByType);

        $this->smarty->assign("list",$listByType);
        $this->smarty->assign("goodsType",$goodsTypeArr);


        $pointRuleURL=C('POINT_RULE_URL');
        if(!$pointRuleURL){
            $pointRuleURL=U('Point/rule');
        }
        $this->smarty->assign('POINT_RULE_URL',$pointRuleURL);

        if($this->isWechat) {
            $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
            //微信分享相关
            $this->smarty->assign('isWxShare', true);
            $this->smarty->assign('wxTitle', "丸子妈妈-积分商城");
            $this->smarty->assign('wxDesc',"积分可免费兑换各类课程、玩具、折扣券等各种福利！");
            $this->smarty->assign('wxLink', $protocol.$_SERVER['HTTP_HOST'].U("index"));
            $this->smarty->assign('wxImage', "http://wzma.176dp.cn/Public/Wap/images/logo.jpg");
            $this->smarty->assign('wxObjType',OBJ_TYPE_POINTMALL);
            $this->smarty->assign('wxObjID',0);
        }








        $this->smarty->assign('title',C('SITE_NAME').'-积分商城');
        $this->smarty->assign('time',time());
        //是否当日首次签到
        signAssignWap($uid,$this->smarty);
        $this->smarty->display('PointMall_v3/index.html');
    }

    
    public function goodsMore(){
        $goodsTypeId=I('get.gid',0,'intval');
        $goodsTypeArr=PointMallModel::getGoodsType();
        if (!array_key_exists($goodsTypeId,$goodsTypeArr)){
            $this->error("指定类型并不存在唷");
        }


        $pointRuleURL=C('POINT_RULE_URL');
        if(!$pointRuleURL){
            $pointRuleURL=U('Point/rule');
        }
        $this->smarty->assign('POINT_RULE_URL',$pointRuleURL);

        $list=PointMallModel::getListByGoodsType($goodsTypeId,1,50);
        $this->smarty->assign('title',$goodsTypeArr[$goodsTypeId]);
        $this->smarty->assign("list",$list['data']);
        $this->smarty->display('PointMall_v3/index_more.html');
    }
    

    public function indexMore(){
        $page = I('post.page',2,'int');
        $uid = session('uid')+0;
        $list = PointMallModel::getOnlineAndPrepareList($page,8,CLIENT_SHOW_WAP,$uid);
        $this->smarty->assign("list",$list['data']);

        $html = $this->smarty->fetch('PointMall_v2/indexMore.html');
        $this->ajaxReturn(array('status'=>0,'dataHTML'=>$html,'page'=>$list['page'],'totalPage'=>$list['totalPage']));
    }
    
    
    
    
    

    /**
     * 往期兑换
     */
    public function endList(){
        $uid = session('uid')+0;
        $list = PointMallModel::getPastList(1,8,CLIENT_SHOW_WAP,$uid);
        $this->smarty->assign("list",$list['data']);
        $this->smarty->assign('title',C('SITE_NAME').'-积分商城');
        //是否当日首次签到

        $this->smarty->display('PointMall_v2/endList.html');
    }

    public function endMore(){
        $uid = session('uid')+0;
        $page = I('post.page',2,'int');

        $list = PointMallModel::getPastList($page,8,CLIENT_SHOW_WAP,$uid);
        $this->smarty->assign("list",$list['data']);

        $html = $this->smarty->fetch('PointMall/indexMore.html');
        $this->ajaxReturn(array('status'=>0,'dataHTML'=>$html,'page'=>$list['page'],'totalPage'=>$list['totalPage']));
    }

    /**
     * exchangeLog 兑换记录 查看用户的兑换记录
     */
    public function exchangeLog(){
        UtilsModel::checkLogin();
        //从session里获得uid
        $uid = session('uid');
        $mReturn=PointMallModel::getExchangeLogList($uid,1,C('LIST_PAGE_SIZE'));

        //是否当日首次签到
        signAssignWap($uid,$this->smarty);

        $exchangeStatus = PointMallModel::getExchangeStatus();
        $this->smarty->assign('title','兑换记录');
        $this->smarty->assign('list', $mReturn['data']);
        $this->smarty->assign('exchangeStatus', $exchangeStatus);
        $this->smarty->display('PointMall_v2/exchangeLog.html');
    }
    /**
     * exchangeLogMore 兑换记录加载更多ajax接口
     */
    public function exchangeLogMore(){
        $p=I('post.p',2,'int');
        //用户uid 从session里获取
        $uid = session('uid');
        $mReturn=PointMallModel::getExchangeLogList($uid,$p,C('LIST_PAGE_SIZE'));
        $pointlog_action = C('OPT_TYPE');
        $this->smarty->assign('action', $pointlog_action);
        $this->smarty->assign('list',$mReturn['data']);
        $result=array(
            'data'=>$this->smarty->fetch('PointMall_v2/exchangeLogItem.html'),
        );
        $this->ajaxReturn($result) ;
    }

    

   



    /**
     * info 积分商城详情页
     */
    public function info(){
        //UtilsModel::checkLogin();

        $id = I('get.id', null);
        $uid=session('uid')+0;
        $pmReturn=PointMallModel::getDetail($id);
        if($pmReturn['status']!=0){
            $this->error($pmReturn['msg']);
        }
        $pointMallItem=$pmReturn['data'];

        //查看次数+1
        PointMallModel::views($id,1,$uid,$pointMallItem['contentType']);

        $joinTimes=PointMallModel::getUserJoinTimes($id,$uid);


        $nowTime = time();
        if($pointMallItem['beginExchangeTimestamp'] > $nowTime){
            $pointMallItem['rest_time'] = $pointMallItem['beginExchangeTimestamp'] - $nowTime;
        }
        //如果结束时间小于当前时间 活动已经结束 overdue => 结束
        if($pointMallItem['endExchangeTimestamp'] < $nowTime){
            $pointMallItem['overdue'] = true;
        }


        $this->smarty->assign('obj', $pointMallItem);

        if($this->isWechat) {
            $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
            //微信分享相关
            $this->smarty->assign('isWxShare', true);
            $this->smarty->assign('wxTitle', $pointMallItem['titleInner']);
            $this->smarty->assign('wxDesc',  $pointMallItem['shareDesc']);
            $this->smarty->assign('wxLink', $protocol.$_SERVER['HTTP_HOST'].U('info?id=' . $id . '&fromUid=' . $uid));
            $this->smarty->assign('wxImage', C('UPLOAD_RESOURCE_PREFIX').$pointMallItem['listPic']);
            $this->smarty->assign('wxObjType',OBJ_TYPE_POINTMALL);
            $this->smarty->assign('wxObjID',$id);
        }


        $this->smarty->assign('POINTMALL_NO_STACK',PointMallModel::POINTMALL_NO_STACK);
        $this->smarty->assign('POINTMALL_NO_LIMIT',PointMallModel::POINTMALL_NO_LIMIT);
        $this->smarty->assign('POINTMALL_NO_LIMIT_QUOTA',PointMallModel::POINTMALL_NO_LIMIT_QUOTA);

        //
        $this->smarty->assign('DELIVERY_SEND',PointMallModel::DELIVERY_SEND);
        $this->smarty->assign('DELIVERY_CARD',PointMallModel::DELIVERY_CARD);
        $this->smarty->assign('DELIVERY_TRAFFIC',PointMallModel::DELIVERY_TRAFFIC);


        //评论列表
        //$commentM=M('comment_'.OBJ_TYPE_POINTMALL,null,DB_MAIN_CFG);
        //$commentList=$commentM->where('objType='.OBJ_TYPE_POINTMALL.' and objID='.$id.' and isDisabled=0')->limit(3)->select();

        //$this->smarty->assign("commentList",$commentList);
        if($uid>0){
            $check=CouponModel::checkNoReceiveWeixinCode($uid);
            if($check['status']==0){
                $this->smarty->assign('hasUnreceive',1);
                $this->smarty->assign('wxCardId',$check['wxCardId']);
                $this->smarty->assign('cardExt',$check['cardExt']);
            }
        }

        //是否当日首次签到
        signAssignWap($uid,$this->smarty);
        
        $mReturn=PointMallModel::getMemberList($id,1,5);
        $members=$mReturn['data'];
        $this->smarty->assign('members',$members);


        $pointRuleURL=C('POINT_RULE_URL');
        if(!$pointRuleURL){
            $pointRuleURL=U('Point/rule');
        }
        $this->smarty->assign('POINT_RULE_URL',$pointRuleURL);
        $this->smarty->assign('qrcode_path',C('WEIXIN_QRCODE_PATH'));//二维码

        $this->smarty->assign('title',$pointMallItem['titleInner']);
        $this->smarty->display('PointMall_v2/pointMallInfo.html');
    }

    /**
     * 活动参与者列表
     * Author: shijy
     */
    public function memberListMore(){
        $id = I('get.id',null,'int');
        $page=I('get.page',1,'int');
        $pageSize=C('LIST_PAGE_SIZE');
        $return=PointMallModel::getMemberList($id,$page,$pageSize);


        $this->smarty->assign('list',$return['data']);
        $html=$this->smarty->fetch('JoinList/join_list_item.html');
        $this->ajaxReturn(array('status'=>0, 'dataHTML'=>$html, 'isPage'=>$return['nextPage']));
    }
    public function memberList(){

        $uid = session("uid")+0;
        //是否当日首次签到
        signAssignWap($uid,$this->smarty);

        $id=I('get.id',0,'int');
        $this->smarty->assign('title','参与者');
        $this->smarty->assign('objID',$id);
        $mReturn=PointMallModel::getMemberList($id,1,10);
        $members=$mReturn['data'];
        $this->smarty->assign('members',$members);
        $this->smarty->display("JoinList/joinList.html");

    }
    
    
    /*
     * validateExchange 积分兑换的兑换验证 ajax接口
     */
    public function validateExchange(){
        $id         = I('get.id',null);
        $eventID    = I('get.eventID',0,'int');   //同城活动编号
        $userlevel  = session('level');
        $uid        = session('uid');
        $return     = PointMallModel::validateExchange($id,$uid,$userlevel,$eventID);
        $this->ajaxReturn($return);
    }




    /*
     * 非秒杀模式下的 积分兑换的兑换ajax接口-----非卡券兑换
     */
    public function doExchange(){
        $id         = I('post.id',null);
        $eventID    = I('post.eventID',0,'int');   //同城活动编号
        $phone      = I('post.phone',null);
        $realname   = I('post.realname',null);
        $address    = I('post.address',null);
        $uid        = session('uid')+0;
        $nickname   = session('nickname');
        $userlevel  = session('level')+0;

        $deliverType=I('post.deliverType',0,'intval');
        switch ($deliverType){
            case PointMallModel::DELIVERY_CARD:
                break;
            case PointMallModel::DELIVERY_PHONE:
                break;
            case PointMallModel::DELIVERY_SEND:
                break;
            default:
                $this->ajaxReturn(array('status'=>1,'msg'=>'错误的参数'));
                break;
        }


        $result=PointMallModel::doExchange($id,$uid,$nickname,$phone,$realname,$address,$eventID,$deliverType,$userlevel);
        $this->ajaxReturn($result);
    }






    /*
     * 下单
     */
    public function exchange_deliverWay(){
        $id = I("get.id",0,'intval');
        if(!$id){
            $this->error('请指定活动');
        }
        $userinfo['bindPhone'] = session('bindPhone');
        $userinfo['userPoint'] = session('point')+0;
        $userinfo['address']   = session('address');
        $userinfo['realName']  = session('realName');

        $data = M("point_mall",null,DB_MAIN_CFG)->where("id={$id}")->field("point,deliveryType,nowPrice,deliverArea,deliverPay,pickAddress,id,cardId")->find();
        $deliveryType = explode(',',$data['deliveryType']);
        $this->smarty->assign('deliveryType',$deliveryType);

        if(in_array(PointMallModel::DELIVERY_PHONE,$deliveryType)){
            $this->smarty->assign('isPhone',true);
        }
        if(in_array(PointMallModel::DELIVERY_CARD,$deliveryType)){
            if($data['cardId']){
                $cardName=CouponModel::getInstance()->where("id={$data['cardId']}")->getField('title');
                $this->smarty->assign('cardName',$cardName);

            }


            $this->smarty->assign('isCard',true);
        }
        if(in_array(PointMallModel::DELIVERY_SEND,$deliveryType)){
            $this->smarty->assign('isExpress',true);
        }

        $this->smarty->assign('DELIVERY_SEND',PointMallModel::DELIVERY_SEND);
        $this->smarty->assign('DELIVERY_PHONE',PointMallModel::DELIVERY_PHONE);
        $this->smarty->assign('DELIVERY_CARD',PointMallModel::DELIVERY_CARD);



        $this->smarty->assign('userInfo',$userinfo);
        $this->smarty->assign('data',$data);
        $this->smarty->display("PointMall_v2/exchange_deliver.html");

    }

    //大转盘抽奖
    public function dialLottery(){
        UtilsModel::checkLogin();
        $uid = session('uid')+0;
        $point = session('point')+0;
        if($point <= 0) $point = 0;
        //获取该页面基础配置-可抽奖次数和每次抽奖扣除积分、转盘图、页面底图、抽奖规则
        $data = PointMallModel::getPageSet($point);
        $this->smarty->assign('data',$data);
        //取底图和活动规则
//        $info = SignLuckyModel::getBasePicAndRegulations(SignLuckyModel::PRIZE_TYPE_DIAL_POINT);
//        $this->smarty->assign('info',$info);
        
        //
//print_r($data['prizeInfo']);die;
        //是否当日首次签到
        signAssignWap($uid,$this->smarty);

        $this->smarty->assign('point',$point);
        $this->smarty->assign('title','幸运大转盘');
        $this->smarty->display('PointMall/dialLottery.html');
    }
}